package com.example.demo.mapimage;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;


public class GetMaps {
	public final static String uurl = "http://restapi.amap.com/v3/staticmap?zoom=14&size=1024*1024&scale=2";// 高德地图api
	 
	/**
	 * 批量爬取地图并保存
	 * 
	 * @param url
	 *            接口地址集合
	 * @param path
	 *            文件保存位置
	 * @return 地图图片获取完成并保存成功返回true
	 */
	public static boolean getMaps(ArrayList<String> url, String path) {
 
		try {
			// 开始时间
			Date begindate = new Date();
			int i = 1;
			for (String ImgSrc : url) {
				// 开始时间
				Date begindate2 = new Date();
				// String imageName = ImgSrc.substring(url.lastIndexOf("/") + 1,
				// ImgSrc.length());
				String imageName = i + ".png";
				do {
				URL uri = new URL(ImgSrc);
				InputStream in = uri.openStream();
				FileOutputStream fo = new FileOutputStream(new File(path + "\\getMaps\\" + imageName));
				byte[] buf = new byte[1024];
				int length = 0;
				// System.out.println("开始下载:" + ImgSrc);
				while ((length = in.read(buf, 0, buf.length)) != -1) {
					fo.write(buf, 0, length);
				}
				in.close();
				fo.close();
				}while(new File(path + "\\getMaps\\" + imageName).length() <= 2000); //文件小于2K说明下载失败,重新下载
				i++;
				System.out.println(imageName + "/" + url.size() + "下载完成");
				// 结束时间
				Date overdate2 = new Date();
				double time = overdate2.getTime() - begindate2.getTime();
				System.out.println("耗时：" + time / 1000 + "s");
			}
			Date overdate = new Date();
			double time = overdate.getTime() - begindate.getTime();
			System.out.println("总耗时：" + time / 1000 + "s");
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("下载失败");
			//开始重新下载
			
			return true;
		}
	}
 
	/**
	 * 计算图片构成信息
	 * 
	 * @param startX
	 * @param startY
	 * @param endX
	 * @param endY
	 * @return
	 */
	public static int[] getCount(String startX, String startY, String endX, String endY) {
		// 解析参数,转换成浮点数,以便后期计算
		Double sx = Double.parseDouble(startX);
		Double sy = Double.parseDouble(startY);
		Double ex = Double.parseDouble(endX);
		Double ey = Double.parseDouble(endY);
 
		// 按照0.005为步长,循环计算出所需获得的图片中心位置坐标
		int countX = (int) ((ex - sx) / 0.005);// 如果ex - sx = 0,说明只有一列
		int countY = (int) ((sy - ey) / 0.005);// 如果sy - ey = 0,说明只有一行
		int[] count = { countY + 1, countX + 1 };
		return count;
	}
 
	/**
	 * 构造接口地址
	 * 
	 * @param startX
	 *            地图左上角x坐标
	 * @param startY
	 *            地图左上角y坐标
	 * @param endX
	 *            地图右下角x坐标
	 * @param endY
	 *            地图右下角y坐标
	 * @param key
	 *            开发者账号的key
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static ArrayList<String> MakeUrls(String startX, String startY, String endX, String endY, String key) {
		// 解析参数,转换成浮点数,以便后期计算
		Double sx = Double.parseDouble(startX);
		Double sy = Double.parseDouble(startY);
		Double ex = Double.parseDouble(endX);
		Double ey = Double.parseDouble(endY);
 
		// 按照0.005为步长,循环计算出所需获得的图片中心位置坐标
		int countX = (int) ((ex - sx) / 0.005);// 如果ex - sx = 0,说明只有一列
		int countY = (int) ((sy - ey) / 0.005);// 如果sy - ey = 0,说明只有一行
		ArrayList<String> url = new ArrayList<String>();
		System.out.println(
				"一共需要获取" + (countY + 1) + "行图片,每行图片共" + (countX + 1) + "张.共" + ((countX + 1) * (countY + 1)) + "图片");
		for (int j = 0; j <= countY; j++) {
			for (int i = 0; i <= countX; i++) {
				System.out.println("X:" + new BigDecimal(sx).setScale(3, BigDecimal.ROUND_HALF_UP) + " Y:"
						+ new BigDecimal(sy).setScale(3, BigDecimal.ROUND_HALF_UP));
				url.add(uurl + "&location=" + new BigDecimal(sx).setScale(3, BigDecimal.ROUND_HALF_UP) + ","
						+ new BigDecimal(sy).setScale(3, BigDecimal.ROUND_HALF_UP) + "&key=" + key);
				sx += 0.005;
			}
			sy -= 0.005;
			sx = Double.parseDouble(startX);
		}
		return url;
	}
}
